﻿#ifndef KEYBOARD_H
#define KEYBOARD_H

#include "AbstractKeyBoard.h"
#include "KeyButton.h"
#include <QLayout>
#include <QListWidget>
#include <QHash>
#include <QMutex>
#include <QPair>
#include <QString>
#include <QLineEdit>


class ChineseWidget : public QListWidget {
    Q_OBJECT
public:
    ChineseWidget(QWidget *parent = NULL);
    void setText(const QString &text);

signals:
    void pressedChanged(const int code, const QString text);

private slots:
    void onItemClicked(QListWidgetItem *item);

private:
    void addOneItem(const QString &text);
    void loadData();

private:
    QMap<QString, QList< QPair<QString, QString> > > m_data;
};

class KeyBoard : public AbstractKeyBoard
{
    Q_OBJECT
public:
    static KeyBoard *getInstance();

    void paintEvent(QPaintEvent *event);
	QLineEdit *lineEdit{ nullptr };

protected:
    void resizeEvent(QResizeEvent *e);

signals:
    void sigFinished(void);

private slots:
    void onSwitchCapsLock();
    void onSwitchSpecialChar();
    void onSwitchEnOrCh();
    void onButtonPressed(const int &code, const QString &text);
    void onClearBufferText();

private:
    static KeyBoard *m_obj; //单例句柄.
    static QMutex m_lock;   //单例安全锁.
    KeyBoard(QWidget *parent);
    KeyButton *createButton(QList<KeyButton::Mode> modes);

    QWidget *createBar(const QList<QList<KeyButton::Mode>> &modes);
    QWidget *chineseBar();
    QHBoxLayout *h1(); //返回布局的
    QHBoxLayout *h2();
    QHBoxLayout *h3();
    QHBoxLayout *h4();
    QWidget *candidateList();
    void resizeButton();

private:
    bool m_isChinese; //是否为中文模式.
    ChineseWidget *m_chineseWidget; //中文候选列表.
    QString m_bufferText;
    QLineEdit *m_curEdit {nullptr}; //当前所编辑的对象.
};

#endif // KEYBOARD_H
